import { initData } from '@/api/data'

export default {
  data() {
    return {
      // loading: true, data: [], pageBean:.pageNo: 0, pageBean.pageSize: 10, total: 0, url: '', params: {}, query: {}, time: 170
      loading: true,
      pageCount: true,
      data: {},
      total: 0,
      url: '',
      params: {
        pageBean: { pageSize: 10, pageNo: 1, totalCount: 0 },
        orderBean: {}
      },
      paramBean: {},
      time: 170
    }
  },
  methods: {
    async init() {
      if (!await this.beforeInit()) {
        return
      }
      if (this.paramBean) {
        this.params['paramBean'] = this.paramBean
      }
      if (!this.pageCount) {
        this.params['pageBean'] = null
      }
      return new Promise((resolve, reject) => {
        this.loading = true
        initData(this.url, this.params).then(res => {
          if (res.data.pageBean) {
            this.total = res.data.pageBean.totalCount
          }
          this.data = res.data
          setTimeout(() => {
            this.loading = false
          }, this.time)
          this.afterInit(res, true)
          resolve(res)
        }).catch(err => {
          this.loading = false
          reject(err)
        })
      })
    },
    afterInit(res) {
    },
    beforeInit() {
      return true
    },
    pageChange(e) {
      this.params.pageBean.pageNo = e
      this.init()
    },
    sizeChange(e) {
      this.params.pageBean.pageNo = 1
      this.params.pageBean.pageSize = e
      this.init()
    }
  }
}
